#include "driver_function.h"

void driver_timer_init()
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode   = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_Period        = 65000;  // ATRLR
    TIM_TimeBaseInitStructure.TIM_Prescaler     = 143;    // PSC 144MHZ / 144 = 1us
    TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStructure);
}

void driver_timer_delay_us(uint16_t us)
{
    TIM_Cmd(TIM6,ENABLE);
    TIM_SetCounter(TIM6,0);
    while(TIM_GetCounter(TIM6) < us);
    TIM_Cmd(TIM6, DISABLE);

}
